package com.wcity.manage.web.weather;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by Administrator on 2016/12/23.
 */
@Controller
public class WeatherAct {

    @RequestMapping("get_weather")
    @ResponseBody
    public Map<String,Object> getWeather (String areaid , String area) throws Exception {
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> map1 = new HashMap<>();
        int appid = 28658;
        String showapi_sign = "a20d8402cb374949a125829896bc8b4b";
        //String area = "杭州";
        //int areaid  = 101210101;
        String strUrl = "http://route.showapi.com/9-2?showapi_appid="+appid+"&area="+area+"&areaid="+areaid+"&showapi_sign="+showapi_sign;
        URL u=new URL(strUrl);
        InputStream in=u.openStream();
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        try {
            byte buf[]=new byte[1024];
            int read = 0;
            while ((read = in.read(buf)) > 0) {
                out.write(buf, 0, read);
            }
        }  finally {
            if (in != null) {
                in.close();
            }
        }
        byte b[]=out.toByteArray( );
        map.put("weather",new String(b,"utf-8"));
        //json转map
        JSONObject jsonObject = JSONObject.parseObject(map.get("weather")+"");
        JSONObject json = JSONObject.parseObject(jsonObject.get("showapi_res_body")+"");
        map1.put("now",json.get("now"));
        return map1;
    }
}
